package cn.net.vote.util;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class PointUtil {
	
	/** 
	 * 【描 述】：判断目标点是否在多边形内(由多个点组成)
	 * @param polygon 多边形二维数组
	 * @param longitude 目标经度
	 * @param latitude 目标纬度
	 * @return
	 */
	public static boolean isPointInPolygon(String polygon, double longitude, double latitude) {
//        String s = "[[[126.50033276705423,45.788118121526125],[126.50775712160745,45.79052138080347],[126.50621216921488,45.79322504749048],[126.49853032259622,45.7907788728689],[126.50033276705423,45.788118121526125]]]";
//        polygon = s;
//		45.786728,126.495978
//		longitude = 126.5024744707;
//		latitude = 45.7902472324;
        JSONArray arr = JSON.parseArray(polygon);
        arr = (JSONArray) arr.get(0);
		double x = longitude, y = latitude;
	    boolean inside = false;
	    for(int i = 0, j = arr.size() - 1; i < arr.size(); j = i++) {
	    	JSONArray jsonI = (JSONArray) arr.get(i);
	    	JSONArray jsonJ = (JSONArray) arr.get(j);
	        double xi = jsonI.getDouble(0), yi = jsonI.getDouble(1);
	        double xj = jsonJ.getDouble(0), yj = jsonJ.getDouble(1);
	        boolean intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
	        if (intersect) {
	        	inside = !inside;
	        }
	    }
		return inside;
	}
	
	
	public static void main(String[] args) {
//		System.out.println(isPointInPolygon(null, 125.1137262582779, 46.59062772989274));
//		System.out.println(isPointInPolygon(null, 125.11152684688568, 46.59006446599961));
		System.out.println(isPointInPolygon("", 0, 0));
	}
}
